﻿define(['durandal/system', 'durandal/plugins/router', 'config', 'services/datacontext', 'services/logger', 'services/helpers'],
    function (system, router, config, ds, logger,helpers) {

        var windowCarHash = ko.observable(false);
        var windowPickUpHash = ko.observable(false);
        var windowVansHash = ko.observable(false);


        window.onhashchange = function () {
            var currentHash = window.location.hash;
            if (currentHash.indexOf('menu-car/') > 0) {
                windowCarHash(true);
                windowPickUpHash(false);
                windowVansHash(false);
            } else if (currentHash.indexOf('menu-pickups/') > 0) {
                windowCarHash(false);
                windowPickUpHash(true);
                windowVansHash(false);
            } else if (currentHash.indexOf('menu-vans/') > 0) {
                windowCarHash(false);
                windowPickUpHash(false);
                windowVansHash(true);
            } else {
                windowCarHash(false);
                windowPickUpHash(false);
                windowVansHash(false);
            }

        };
        
        
        var carDropdownMenu = ko.computed(function () {
            return router.allRoutes().filter(function (r) {
                return r.settings.carSubMenu;
            });
        });

        var pickupDropdownMenu = ko.computed(function() {
            return router.allRoutes().filter(function(r) {
                return r.settings.pickupSubMenu;
            });

        });

        var vansDropdownMenu = ko.computed(function () {
            return router.allRoutes().filter(function (r) {
                return r.settings.vansSubMenu;
            });

        });

        //#region Internal Methods
        function activate() {
            //return ds.getPrimeData()
            //    .then(boot);
            helpers.activateThemeConfig();
            return boot();
        }
        
        var viewAttached = function (view) {
            helpers.activateThemeConfig();
        };

        function boot() {
            router.map(config.routes);
            log('Hot Towel SPA Loaded!', null, true);
            return router.activate(config.startAppModule);
        }

        function selectSubMenu(data,event) {
            router.navigateTo(event.target.hash);
        }
        
        function selectParentMenu(item) {
            router.navigateTo(item.hash);
        }

        function log(msg, data, showToast) {
            logger.log(msg, data, system.getModuleId(shell), showToast);
        }
        //#endregion
        
        var shell = {
            activate: activate,
            viewAttached: viewAttached,
            router: router,
            carDropdownMenu : carDropdownMenu,
            pickupDropdownMenu: pickupDropdownMenu,
            vansDropdownMenu: vansDropdownMenu,
            windowCarHash: windowCarHash,
            windowPickUpHash: windowPickUpHash,
            windowVansHash: windowVansHash,
            selectSubMenu: selectSubMenu,
            selectParentMenu: selectParentMenu
        };

        return shell;

    });